{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Dealing with Outliers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Sometimes outliers can mess up an analysis; you usually don't want a handful of data points to skew the overall results. Let's revisit our example of income data, with Donald Trump thrown in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAEJCAYAAAB/pOvWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEU9JREFUeJzt3X+s3XV9x/Hny1YY6lCwXcMKrJ3rNIXIlI51agyOP6i6\nrLgpqVPpTEOzwIxbZibsj7ksqYPEbI5ssHSolM3IOiWj2cSNVBkuCuwyQShY6USkFWj9yeYytPDe\nH+fDdrifW3p7z+09ve3zkZycz/l8P5/P+XzS5r7u9/s953NTVUiSNOx5456AJOnIYzhIkjqGgySp\nYzhIkjqGgySpYzhIkjqGgySpYzhIkjqGgySps3DcE5ipRYsW1bJly8Y9DUmaV+66665vVdXig7Wb\nt+GwbNkyJiYmxj0NSZpXkjw8nXZeVpIkdQwHSVLHcJAkdQwHSVLHcJAkdQ4aDkk+mmRvkvuG6k5O\nckuSB9vzSUPHLk+yK8nOJOcP1Z+d5N527KokafXHJ/nbVn9HkmWzu0RJ0qGazpnDdcCaSXWXAdur\nagWwvb0myUpgHXBG63N1kgWtzzXAxcCK9nhmzA3Ad6vqZ4A/Ba6c6WIkSbPjoOFQVbcB35lUvRbY\n0spbgAuG6m+oqier6iFgF3BOklOAE6vq9hr8XdLrJ/V5ZqxPAuc9c1YhSRqPmd5zWFJVj7byY8CS\nVl4KPDLUbnerW9rKk+uf1aeq9gPfB146w3lJkmbByN+QrqpKUrMxmYNJshHYCHD66afPeJxll/3j\nl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2MH/\n33g+F9iZ5KvAEmDTIb+fG+9JkibzzEGS1DEcJEkdw0GS1DEcJEkdw0GS1DEcJEkdw0GS1DEcJEmd\n/wW8L+YMtJCLMgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x279597c5358>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "\n",
    "incomes = np.random.normal(27000, 15000, 10000)\n",
    "incomes = np.append(incomes, [1000000000])\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.hist(incomes, 50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "That's not very helpful to look at. One billionaire ended up squeezing everybody else into a single line in my histogram. Plus it skewed my mean income significantly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "126713.54327205669"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "incomes.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "It's important to dig into what is causing your outliers, and understand where they are coming from. You also need to think about whether removing them is a valid thing to do, given the spirit of what it is you're trying to analyze. If I know I want to understand more about the incomes of \"typical Americans\", filtering out billionaires seems like a legitimate thing to do.\n",
    "\n",
    "Here's something a little more robust than filtering out billionaires - it filters out anything beyond two standard deviations of the median value in the data set:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAE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lsGgcV7267yfujnFWxi8osLpl3G4rN0O\nnab+KedU1QngB8DPn+FnzQfzuW2n1f6FfRFwN7C0qo60XUeBpa08m8/fufB+4J3Aj4fqeujbKuA4\n8LE2LfXhJBexwPtWVd8F/hL4L+AI8IOq+hfmab/Oi6BP8sU2j3bqbf3QMe8GTgA3z11LNZUkzwI+\nBbyjqn44vK8GQ58FdxlZktcBx6rqnsmOWah9YzASfTHwt1X1IuB/GExp/MRC7FuSXwL+iMEL2XOB\ni5K8cfiY+dSv8+KLR6rqN8+0P8mbgNcBa9uTA5Mv43C4lU+tHz7nUJILGPzb+p1Wf/Up59w1/Z6M\nxZTLVcwXSZ7GIORvrqpPt+rHkiyrqiNJlgHHWv1sPn/j9lLg9UleAzwD+LkkH6ePvh0CDlXV3W37\nNgZBv9D7NgH8R1UdB0jyaeDXmaf9Oi9G9GeSZB2Df5lfX1X/O7RrJ7ChvfO9ClgN7G7/lv0wyUva\nu+O/x2C++Ilznrii5neAL7UXjjuAVye5pL0L/+pWNx8siOUq2u/6I8C+qnrf0K7h3/lGnvpczNbz\nN1ZVtaWqllfVSga//y9V1Rs76dtR4GCS57eqtQyWK1/ofdsPvCTJz7b2rAX2zdt+jfMNi4VwAw4w\nmAe7t90+OLTv3QzeHd9Peye81U8A97V9f82THzx7BvCP7WfuBp43dM5bWv0B4M1z3e9TfgevYfDG\n0reAd891eyZp48sY/Bv89aHn6jUM5ix3AQ8CXwQWj+P5O4f9vJon34ztom/AC4E97bn7JwZXni34\nvgHvYvCidR/wDwyuqJmX/fKTsZLUufN+6kaSemfQS1LnDHpJ6pxBL0mdM+glqXMGvSR1zqCXpM4Z\n9JLUuf8HxDzT6rZ6sbQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2795b8394e0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def reject_outliers(data):\n",
    "    u = np.median(data)\n",
    "    s = np.std(data)\n",
    "    filtered = [e for e in data if (u - 2 * s < e < u + 2 * s)]\n",
    "    return filtered\n",
    "\n",
    "filtered = reject_outliers(incomes)\n",
    "\n",
    "plt.hist(filtered, 50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "That looks better. And, our mean is more, well, meangingful now as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26726.214626383888"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(filtered)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Activity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Instead of a single outlier, add several randomly-generated outliers to the data. Experiment with different values of the multiple of the standard deviation to identify outliers, and see what effect it has on the final results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
